#!/bin/bash
echo "===================================================="
echo "deploy backend start"
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
cd "$SCRIPT_DIR"

ROOT_DIR=$(dirname $(dirname "$SCRIPT_DIR"))
PLUGIN_DIR=$ROOT_DIR/plugins
BACKEND_DIR=$ROOT_DIR/wex-backend

# 整合扩展到项目中
echo "start add plugins..."
for plugin_dir in $(ls -d $PLUGIN_DIR); do
    if [ -d "$plugin_dir/backend" ]; then
        plugin_name=$(basename $plugin_dir)
        target_dir="$BACKEND_DIR/plugin/plugins/$plugin_name"
        mkdir -p "$target_dir"
        cp -r "$plugin_dir/backend/"* "$target_dir"
        echo "Moved plugin $plugin_name to $target_dir"
    fi
done
echo "add plugins done"

echo "Deploy backend server..."
cd $BACKEND_DIR
cp -r $SCRIPT_DIR/env.py $BACKEND_DIR/wex/env.py
pip install -r requirements.txt
python3 manage.py migrate
python3 manage.py init_system
pm2 reload ecosystem.config.js
python3 manage.py deploy_record
echo "Done"

echo "deploy backend end"
echo "===================================================="